﻿Public Class ctrlThemXoaSuaNhanVien

    Private Sub btnThem_Click(sender As Object, e As EventArgs) Handles btnThem.Click
        Dim frm As New frmThemNhanVien
        AddHandler frm.ThemThanhCongNhanVien, AddressOf CapNhat
        frm.ShowDialog()
    End Sub

    Private Sub btnXoa_Click(sender As Object, e As EventArgs) Handles btnXoa.Click
        If CtrlDGVNhanVien1.bsNhanVien.Current IsNot Nothing Then
            If ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Bạn muốn xoá nhân viên " + _
                                CType(CtrlDGVNhanVien1.bsNhanVien.Current, tbNhanVien).TenDangNhap + "?" + _
                                vbNewLine + "Mọi dữ liệu liên quan sẽ bị xoá theo?", "Xác Nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
                            = DialogResult.Yes Then
                dt.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, dt.tbNhanViens)
                CtrlDGVNhanVien1.bsNhanVien.RemoveCurrent()
                dt.SubmitChanges()
            End If
        End If
    End Sub

    Private Sub btnLuuLai_Click(sender As Object, e As EventArgs) Handles btnLuuLai.Click
        CtrlNhanVien1.KryptonLabel1.Focus()
        If btnSua.Checked = False Then Exit Sub
        dt.Refresh(Data.Linq.RefreshMode.OverwriteCurrentValues, dt.tbNhanViens)
        If CtrlNhanVien1.txtMatKhau.Text = "" Then
            ComponentFactory.Krypton.Toolkit.KryptonMessageBox.Show("Mật khẩu không được để trống!", "Thông Báo", _
                                                                     MessageBoxButtons.OK, MessageBoxIcon.Information)
            CtrlNhanVien1.txtMatKhau.Focus()
            Exit Sub
        End If

        dt.SubmitChanges()
        btnSua.Checked = False
    End Sub

    Private Sub txtTuKhoa_KeyDown(sender As Object, e As KeyEventArgs) Handles txtTuKhoa.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnTimKiem_ButtonClick(Nothing, Nothing)
        End If
    End Sub

    Private Sub txtTuKhoa_TextChanged(sender As Object, e As EventArgs) Handles txtTuKhoa.TextChanged
        If txtTuKhoa.Text = "" Then
            CapNhat()
        End If
    End Sub

    Private Sub btnTimKiem_ButtonClick(sender As Object, e As EventArgs) Handles btnTimKiem.ButtonClick
        Dim key As String = mdlGlobals.BoDauTiengViet(txtTuKhoa.Text.Trim.ToLower)
        If TênDangNhapToolStripMenuItem.Checked Then
            CtrlDGVNhanVien1.bsNhanVien.DataSource = _
                dt.tbNhanViens.Where(Function(s) dt.f_nosymbol(s.TenDangNhap).ToLower.Contains(key))
        ElseIf TênCửaHàngToolStripMenuItem.Checked Then
            CtrlDGVNhanVien1.bsNhanVien.DataSource = _
                dt.tbNhanViens.Where(Function(s) dt.f_nosymbol(s.tbCuaHang.TenCuaHang).ToLower.Contains(key))
        End If
    End Sub

    Private Sub TênDangNhapToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TênDangNhapToolStripMenuItem.Click
        TênCửaHàngToolStripMenuItem.Checked = False
        TênDangNhapToolStripMenuItem.Checked = True
    End Sub

    Private Sub TênCửaHàngToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles TênCửaHàngToolStripMenuItem.Click
        TênCửaHàngToolStripMenuItem.Checked = True
        TênDangNhapToolStripMenuItem.Checked = False
    End Sub

    Private Sub HuỷTìmKiếmToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles HuỷTìmKiếmToolStripMenuItem.Click
        CapNhat()
    End Sub

    Private Sub btnQuayLai_Click(sender As Object, e As EventArgs) Handles btnQuayLai.Click
        undoChange()
        CapNhat()
    End Sub

    Private Sub btnCapNhat_Click(sender As Object, e As EventArgs) Handles btnCapNhat.Click
        CapNhat()
    End Sub

    Private Sub ctrlThemXoaSuaNhanVien_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        CapNhat()
    End Sub

    Private Sub CapNhat()
        CtrlDGVNhanVien1.bsNhanVien.DataSource = From nv In dt.tbNhanViens
                                                 Select nv Order By nv.TenNhanVien Ascending

        btnSua.Checked = False
    End Sub

    Private Sub CtrlDGVNhanVien1_NhanVienThayDoi(nhanVien As tbNhanVien) Handles CtrlDGVNhanVien1.NhanVienThayDoi
        CtrlNhanVien1.NhanVien = nhanVien
    End Sub

    Private Sub CtrlDGVNhanVien1_SuaNhanVien() Handles CtrlDGVNhanVien1.SuaNhanVien
        btnSua_CheckedChanged(Nothing, Nothing)
    End Sub

    Private Sub CtrlDGVNhanVien1_ThemNhanVien() Handles CtrlDGVNhanVien1.ThemNhanVien
        btnThem_Click(Nothing, Nothing)
    End Sub

    Private Sub CtrlDGVNhanVien1_XoaNhanVien() Handles CtrlDGVNhanVien1.XoaNhanVien
        btnXoa_Click(Nothing, Nothing)
    End Sub

    Private tenDanhNhap As String
    Private Sub btnSua_CheckedChanged(sender As Object, e As EventArgs) Handles btnSua.CheckedChanged
        If CtrlDGVNhanVien1.bsNhanVien.Current IsNot Nothing Then
            CtrlNhanVien1.Enabled = Not CtrlNhanVien1.Enabled
            tenDanhNhap = CtrlNhanVien1.txtTenDangNhap.Text
            CtrlNhanVien1.txtTenDangNhap.ReadOnly = True
        End If
    End Sub
End Class
